﻿/*
 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

#ifndef CHECK_COM_H
#define CHECK_COM_H

#include <QTimer>
#include "SerialPort/PortManager.h"
#include "BurnToolCommon/Global.h"
#include "BurnToolCommon/StringClass.h"
namespace BurnDlg {
    enum class CheckComWiteThreadMsg {
        WITE_THREAD_MSG_STARTCHECKCOM = WM_USER + 0x111,
        WITE_THREAD_MSG_STOPCHECKCOM,
        WITE_THREAD_MSG_NEXTCHECKCOM,
    };

    enum class CheckComFsm {
        CHECKCOM_FSM_NONE,
        CHECKCOM_FSM_DOING,
    };
}
class CheckCom : public QObject {
    Q_OBJECT
public:
    explicit CheckCom(QObject *pParent = nullptr);
    ~CheckCom() final;
public:
    void StartCheck();
    void RefreshComList(QStringList &result) const;
    bool ConfigComPort(int com);
    UINT CommWiteThread(unsigned int msgId, WPARAM srcData, LPARAM srcLen);
    void ProcessReceiveData(PBYTE data, unsigned int &dataLen);
    void NextCheck();
    void KillMyTimer();
    void SaveResult() const;
    bool IsBusy() const;
    bool IsApplyResult() const;
    void SetApplyResult(bool result);
    QString GetStrResult();
protected slots:
    LONG SlotCommunication(WPARAM srcData, LPARAM srcLen);
    void SlotBurnCtrlCommonTimerHandler();
private:
    BOOL witeThreadAlive;
    unsigned int witeThreadMsgId;
    PortManager portManager;
    unsigned char dataArr[MAX_BUFFER_LEN] = { 0 };
    unsigned int u32dataLen = 0;
    QStringList comList;
    unsigned int comIndex;
    QStringList outputPosition;
    QStringList outputCom;
    bool isApplyResult;
    QString strResult;
    BurnDlg::CheckComFsm fsm;
    unsigned int ctrlPeriod;

    unsigned int baud; // 波特率115200
    int comNum;             // 串口号
    QSerialPort::Parity cParity;          // 校验
    unsigned int databits;  // 数据位8
    unsigned int stopbits;  // 停止位 /* 0,1,2 = 1, 1.5, 2 */
    unsigned int flowctrl;   // 流控

    QTimer *sendCtrlTimer;
    U32 sendCtrlTimerEvent;
    MyStringClass::StringClass stringClass;
};
#endif // !CHECK_COM_H
